package com.taotao.manager.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.common.bean.EasyUIResult;
import com.taotao.manager.service.ItemParamService;
import com.taotao.pojo.TbItemParam;

@RequestMapping("/item/param")
@Controller
public class ItemParamController {
	private static final Logger LOGGER = LoggerFactory.getLogger(ItemParamController.class);
	
	@Autowired
	private ItemParamService itemParamService;
	
	@RequestMapping(value = "/list" ,method = RequestMethod.GET)
	public ResponseEntity<EasyUIResult> queryItemParamList(
			@RequestParam("page")int pages,@RequestParam("rows")int rows){
		try {
			EasyUIResult result = this.itemParamService.queryItemParamList(pages, rows);
			return ResponseEntity.ok(result);
		} catch (Exception e) {
			LOGGER.error("查询商品规格参数失败", e);
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
	/***
	 * 根据商品类目ID查询是否存在规格参数模板
	 * <p>Title: queryItemParamById</p>
	 * <p>Description: </p>
	 * @param itemCatId
	 * @return
	 */
	@RequestMapping(value="{itemCatId}",method=RequestMethod.GET)
	public ResponseEntity<TbItemParam> queryItemParamById(@PathVariable("itemCatId") long itemCatId){
		try{
			TbItemParam tbItemParam = new TbItemParam();
			tbItemParam.setItemCatId(itemCatId);
			TbItemParam result = itemParamService.queryOne(tbItemParam);
			 
			if(result == null) {
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			return ResponseEntity.ok(result);
		}catch (Exception e) {
			LOGGER.error("查询商品类目对应的规格参数模板失败,itemCatId = "+itemCatId,e);
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	} 
	
	/**
	 * 新增规格参数模板
	 * <p>Title: saveItemParam</p>
	 * <p>Description: </p>
	 * @param itemCatId
	 * @param paramData
	 * @return
	 */
	@RequestMapping(value="{itemCatId}" ,method = RequestMethod.POST)
	public ResponseEntity<Void> saveItemParam(@RequestParam(value = "{itemCatId}") long itemCatId,
			@RequestParam(value = "paramData") String paramData) {
		try {
			TbItemParam tbItemParam = new TbItemParam();
			tbItemParam.setItemCatId(itemCatId);
			tbItemParam.setParamData(paramData);
			boolean result = this.itemParamService.save(tbItemParam) > 0 ? true:false;
			if(result) {
				  return ResponseEntity.status(HttpStatus.CREATED).build();
			}
		} catch (Exception e) {
			LOGGER.error("新增商品类目规格参数模板失败",e);
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
}
